
package server.events;



import client.MapleCharacter;
import client.MapleStat;
import server.Timer;
import server.maps.MapleMap;
import tools.MaplePacketCreator;
import tools.Pair;

import java.util.Map;
import java.util.concurrent.ScheduledFuture;










 public class MapleOxQuiz
         extends MapleEvent
         {
       private ScheduledFuture<?> oxSchedule;
       private ScheduledFuture<?> oxSchedule2;
    /*  17 */   private int timesAsked = 0;
       private boolean finished = false;



    public MapleOxQuiz(int channel, MapleEventType type) {
        /*  21 */
        super(channel, type);

    }




    public void finished(MapleCharacter chr) {
    }




    private void resetSchedule() {
        /*  29 */
        if (this.oxSchedule != null) {
            /*  30 */
            this.oxSchedule.cancel(false);
            /*  31 */
            this.oxSchedule = null;

        }
        /*  33 */
        if (this.oxSchedule2 != null) {
            /*  34 */
            this.oxSchedule2.cancel(false);
            /*  35 */
            this.oxSchedule2 = null;

        }

    }




    public void onMapLoad(MapleCharacter chr) {
        /*  41 */
        super.onMapLoad(chr);
        /*  42 */
        if (chr.getMapId() == this.type.mapids[0] && !chr.isGM()) {
            /*  43 */
            chr.canTalk(false);

        }

    }




    public void reset() {
        /*  49 */
        super.reset();
        /*  50 */
        getMap(0).getPortal("join00").setPortalState(false);
        /*  51 */
        resetSchedule();
        /*  52 */
        this.timesAsked = 0;

    }




    public void unreset() {
        /*  57 */
        super.unreset();
        /*  58 */
        getMap(0).getPortal("join00").setPortalState(true);
        /*  59 */
        resetSchedule();

    }





    public void startEvent() {
        /*  65 */
        sendQuestion();
        /*  66 */
        this.finished = false;

    }



    public void sendQuestion() {
        /*  70 */
        sendQuestion(getMap(0));

    }



    public void sendQuestion(MapleMap toSend) {
        /*  74 */
        Map.Entry<Pair<Integer, Integer>, MapleOxQuizFactory.MapleOxQuizEntry> question = MapleOxQuizFactory.getInstance().grabRandomQuestion();
        /*  75 */
        if (this.oxSchedule2 != null) {
            /*  76 */
            this.oxSchedule2.cancel(false);

        }
        /*  78 */
        this.oxSchedule2 = Timer.EventTimer.getInstance().schedule(() -> {
            int number = 0;
            for (MapleCharacter mc : toSend.getCharactersThreadsafe()) {
                if (mc.isGM() || !mc.isAlive()) number++;
            }
            if (toSend.getCharactersSize() - number <= 1 || this.timesAsked == 10) {
                toSend.broadcastMessage(MaplePacketCreator.serverNotice(6, "The event has ended"));
                unreset();
                for (MapleCharacter chr : toSend.getCharactersThreadsafe()) {
                    if (chr != null && !chr.isGM() && chr.isAlive()) {
                        chr.canTalk(true);
                        chr.finishAchievement(19);
                        givePrize(chr);
                        warpBack(chr);
                    }
                }
                this.finished = true;
                return;
            }
            toSend.broadcastMessage(MaplePacketCreator.showOXQuiz(((Integer) ((Pair) question.getKey()).left).intValue(), ((Integer) ((Pair) question.getKey()).right).intValue(), true));
            toSend.broadcastMessage(MaplePacketCreator.getClock(10));
        }, 10000L);
























        /* 103 */
        if (this.oxSchedule != null) {
            /* 104 */
            this.oxSchedule.cancel(false);

        }
        /* 106 */
        this.oxSchedule = Timer.EventTimer.getInstance().schedule(() -> {
            if (this.finished) return;
            toSend.broadcastMessage(MaplePacketCreator.showOXQuiz(((Integer) ((Pair) question.getKey()).left).intValue(), ((Integer) ((Pair) question.getKey()).right).intValue(), false));
            this.timesAsked++;
            for (MapleCharacter chr : toSend.getCharactersThreadsafe()) {
                if (chr != null && !chr.isGM() && chr.isAlive()) {
                    if (!isCorrectAnswer(chr, ((MapleOxQuizFactory.MapleOxQuizEntry) question.getValue()).getAnswer())) {
                        chr.getStat().setHp(0, chr);
                        chr.updateSingleStat(MapleStat.HP, 0L);
                        continue;
                    }
                    chr.gainExp(3000L, true, true, false);
                }
            }
            sendQuestion();
        }, 20000L);

    }




















    private boolean isCorrectAnswer(MapleCharacter chr, int answer) {
        /* 127 */
        double x = chr.getTruePosition().getX();
        /* 128 */
        double y = chr.getTruePosition().getY();
        /* 129 */
        if ((x > -234.0D && y > -26.0D && answer == 0) || (x < -234.0D && y > -26.0D && answer == 1)) {
            /* 130 */
            chr.dropMessage(6, "[Ox Quiz] Correct!");
            /* 131 */
            return true;

        }
        /* 133 */
        chr.dropMessage(6, "[Ox Quiz] Incorrect!");
        /* 134 */
        return false;

    }

}


/* Location:              C:\PGX\Server.jar!\server\events\MapleOxQuiz.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */